﻿using System;
using System.Web.UI.WebControls;
using SalePartial.BLL;
using XunPan.Web;
using SalePartial.Utility;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI;

public partial class ManageRoles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //修正导航
            //HyperLink hlkRole = (HyperLink)Master.FindControl("Content2").FindControl("hlkRole");
            //hlkRole.CssClass = "Choice";

            //HyperLink hlkUser = (HyperLink)Master.FindControl("hlkUser");
            //hlkUser.CssClass = "";

            //HyperLink hlkPower = (HyperLink)Master.FindControl("hlkPower");
            //hlkUser.CssClass = "";

            int ckint = 0;

            //DateTime ckbtime, cketime;

            // 检查用户权限并存入使用记录。
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                Response.Redirect(GlobalSetting.UrlHomePage, true);
            }

           DataSet ds = UserBLL.Select_Role( null, null, null);
           gv_Role.DataSource = ds;
           gv_Role.DataBind();
            
        }
        
    }

    // Check_Power() 检查用户权限
    private void check_power(string f_power)
    {
        // 载入公共函数

        // 若 Session 不存在则直接显示错误消息
        try
        {

            if (SecurityUtility.Check_Power(Session["UserName"].ToString(), Session["mg_power"].ToString(), f_power) > 0)
                Response.Redirect("../Error.aspx?ErrCode=1");
        }
        catch
        {
            Response.Redirect("../Error.aspx?ErrCode=2");
        }
    }

    protected void gv_Role_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        //lb_pageid.Text = gv_Role.PageIndex.ToString();
    }

    protected void Btn_Set_Click(object sender, EventArgs e)
    {
        DataSet ds = UserBLL.Select_Role(tb_mg_id.Text, tb_RoleName.Text, tb_Description.Text);
        gv_Role.DataSource = ds;
        gv_Role.DataBind();
    }

    protected void gv_Role_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadTextBox rtbRoleNo = (RadTextBox)editMan.GetColumnEditor("mg_id").ContainerControl.FindControl("rtbRoleNo");
        RadTextBox rtbRoleName = (RadTextBox)editMan.GetColumnEditor("RoleName").ContainerControl.FindControl("rtbRoleName");
        RadTextBox rtbDescription = (RadTextBox)editMan.GetColumnEditor("Description").ContainerControl.FindControl("rtbDescription");

        DataSet ds = UserBLL.Select_Role(null, rtbRoleName.Text, null);

        if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
           gv_Role.Controls.Add(new LiteralControl("此‘角色名称’已经使用，请重新设置!"));
            
            return;
        }

        UserBLL.UpdateRole(rtbRoleNo.Text.Trim(),rtbRoleName.Text.Trim(), rtbDescription.Text.Trim(), "");
    }


    protected void gv_Role_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        //RadTextBox rtbRoleNo = (RadTextBox)editMan.GetColumnEditor("mg_id").ContainerControl.FindControl("rtbRoleNo");ning
        RadTextBox rtbRoleName = (RadTextBox)editMan.GetColumnEditor("RoleName").ContainerControl.FindControl("rtbRoleName");
        RadTextBox rtbDescription = (RadTextBox)editMan.GetColumnEditor("Description").ContainerControl.FindControl("rtbDescription");
        //DataSet ds = UserBLL.Select_Role(null, rtbRoleName.Text, rtbDescription);


        DataSet ds = UserBLL.Select_Role(null, rtbRoleName.Text, null);//ning 修改，内容不可重复，避免不能对号入座
        if(ds != null && ds.Tables!= null && ds.Tables.Count>0 && ds.Tables[0].Rows.Count > 0)
        {
            //Response.Write("<script language=javascript>  alert('此‘角色名称’已经使用，请重新设置!');</script>");
            gv_Role.Controls.Add(new LiteralControl("此‘角色名称’已经使用，请重新设置!"));
            
            
            return;
        }



        //ning start
        DataSet ds1 = UserBLL.Select_Role(null, null, rtbDescription.Text);//ning 修改，内容不可重复，避免不能对号入座
        if (ds1 != null && ds1.Tables != null && ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
        {
               gv_Role.Controls.Add(new LiteralControl("此‘角色身份’已经使用，请重新设置!"));


            return;
        }


        //ning end

        UserBLL.AddRole(rtbRoleName.Text.Trim(), rtbDescription.Text.Trim(), "");

        ds = UserBLL.Select_Role(tb_mg_id.Text, tb_RoleName.Text, tb_Description.Text);
        gv_Role.DataSource = ds;
        gv_Role.DataBind();
    }


    protected void gv_Role_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        string RoleId = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RoleId"].ToString();


        UserBLL.DeleteRole(RoleId);

        DataSet ds = UserBLL.Select_Role(tb_mg_id.Text, tb_RoleName.Text, tb_Description.Text);
        gv_Role.DataSource = ds;
        gv_Role.DataBind();

    }

    protected void gv_Role_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        DataSet ds = UserBLL.Select_Role(tb_mg_id.Text, tb_RoleName.Text, tb_Description.Text);
        gv_Role.DataSource = ds;
    }
}
